package com.example.carrierapp.ui.view;

import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;

import java.util.Timer;
import java.util.TimerTask;

public class CountdownTextView extends android.support.v7.widget.AppCompatTextView {
    static long mSeconds;
    TimerTask mTimerTask;
    final static int what_count_down_tick = 1;
    String TAG = "CountdownTextView";

    public CountdownTextView(Context context) {
        super(context);
    }

    public CountdownTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CountdownTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    /**
     * @param seconds
     */
    public void init(long seconds) {
        mSeconds = seconds; //设置总共的秒数
        mTimerTask = new TimerTask() {
            @Override
            public void run() {

                if (mSeconds > 0) {
                    mSeconds--;
                    mHandler.sendEmptyMessage(what_count_down_tick);
                } else {
                    mHandler.removeMessages(what_count_down_tick);
                    mTimerTask.cancel();
                }
            }
        };
    }

    public void init(long seconds, OnTimeEndListener listener) {
        mOnTimeEndListener = listener;
        mSeconds = seconds; //设置总共的秒数
        mTimerTask = new TimerTask() {
            @Override
            public void run() {

                if (mSeconds > 0) {
                    mSeconds--;
                    mHandler.sendEmptyMessage(what_count_down_tick);
                } else {
                    mHandler.removeMessages(what_count_down_tick);
                    mTimerTask.cancel();
                }
            }
        };
    }

    public void start() {
        Timer timer = new Timer();
        timer.schedule(mTimerTask, 0, 1000);
    }

    public void stop() {
        if (mTimerTask != null)
            mTimerTask.cancel();
    }

    private Handler mHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case what_count_down_tick:
                    if (mSeconds <= 0) {
                        setText(String.format("00:00:00"));
                        if (mOnTimeEndListener != null) {
                            mOnTimeEndListener.onTimeEnd();
                        }

                    } else {
//                        Log.e(TAG, "mSeconds=" + mSeconds + "#what_count_down_tick:" + second2TimeSecond(mSeconds) + "#" + String.format(second2TimeSecond(mSeconds)));
                        setText(second2TimeSecond(mSeconds));
                    }
                    break;
            }
        }
    };

    /**
     * 转化为 hh:mm:ss 格式
     *
     * @param second
     * @return
     */
    private String second2TimeSecond(long second) {
        long hours = second / 3600;
        long minutes = (second % 3600) / 60;
        long seconds = second % 60;

        String hourString = "";
        String minuteString = "";
        String secondString = "";
        if (hours < 10) {
            hourString = "0";
            if (hours == 0) {
                hourString += "0";
            } else {
                hourString += String.valueOf(hours);
            }
        } else {
            hourString = String.valueOf(hours);
        }
        if (minutes < 10) {
            minuteString = "0";
            if (minutes == 0) {
                minuteString += "0";
            } else {
                minuteString += String.valueOf(minutes);
            }
        } else {
            minuteString = String.valueOf(minutes);
        }
        if (seconds < 10) {
            secondString = "0";
            if (seconds == 0) {
                secondString += "0";
            } else {
                secondString += String.valueOf(seconds);
            }
        } else {
            secondString = String.valueOf(seconds);
        }
        return hourString + ":" + minuteString + ":" + secondString;
    }

    public interface OnTimeEndListener {
        void onTimeEnd();
    }

    private OnTimeEndListener mOnTimeEndListener;

    public void setOnTimeEndListener(OnTimeEndListener listener) {
        mOnTimeEndListener = listener;
    }
}
